﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using pwblog.Entities;
using pwblog.BusinessObject;
using System.IO;
using pwblog.Common;

namespace pwblog.Web
{
    public partial class Default : System.Web.UI.Page
    {
        ArticleBo articleBo;
        public Article[] Articles
        {
            get {
                return new ArticleBo().GetArticles();
            }
        }

        public int NowViewing
        {
            get { 
                object pageCurrent = ViewState["NowViewing"];
                if (pageCurrent == null)
                    return 0;
                else
                    return (int)pageCurrent;
            }

            set
            {
                ViewState["NowViewing"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "PWBlog || Home";
            if (!Page.IsPostBack)
            {
                articleBo = new ArticleBo();
                FillRepeater(Paging.None);
                //Article[] articles = new ArticleBo().GetArticles();
                ////ArticleXml[] articles = new ArticleXmlBo().GetArticlesFromXml();
                //if (articles != null)
                //{
                //    articleRepeater.DataSource = articles;
                //    articleRepeater.DataBind();
                //}
            }
        }

        private void FillRepeater(Paging paging)
        {
            PagedDataSource pagedDataSource = new PagedDataSource();

            // assign den du lieu
            pagedDataSource.DataSource = articleBo.FormatDate(Articles);

            // cho phep phan trang
            pagedDataSource.AllowPaging = true;
                
            // thiet lap so item trong 1 trang
            pagedDataSource.PageSize = 5;

            switch (paging)
            {
                case Paging.Next:
                    {
                        NowViewing++;
                        break;
                    }
                case Paging.Previous:
                    {
                        NowViewing--;
                        break;
                    }
                case Paging.Last:
                    {
                        NowViewing = pagedDataSource.PageCount - 1;
                        break;
                    }
                default:
                    {
                        NowViewing = 0;
                        break;
                    }
            }

            // thiet vi tri trang hien tai
            pagedDataSource.CurrentPageIndex = NowViewing;

            // an nut Next, Previous luc can thiet
            lbntFirst.Visible = !pagedDataSource.IsFirstPage;
            lbtnNext.Visible = !pagedDataSource.IsLastPage;
            lbntPrevious.Visible = !pagedDataSource.IsFirstPage;
            lbntLast.Visible = !pagedDataSource.IsLastPage;
            
            //Assign PagedDataSource to repeater
            articleRepeater.DataSource = pagedDataSource;
            articleRepeater.DataBind();
        }


        protected void lbntPrevious_Click(object sender, EventArgs e)
        {
            FillRepeater(Paging.Previous);
        }

        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            FillRepeater(Paging.Next);
        }

        protected void lbtnLast_Click(object sender, EventArgs e)
        {
            FillRepeater(Paging.Last);
        }

        protected void lbntFirst_Click(object sender, EventArgs e)
        {
            FillRepeater(Paging.First);
        }
    }
}